package cn.lili.eventbus.handlers;

import cn.lili.consumer.event.*;
import cn.lili.eventbus.event.MemberEvent;
import cn.lili.modules.connect.entity.dto.MemberConnectLoginMessage;
import cn.lili.modules.member.entity.dos.Member;
import cn.lili.modules.member.entity.dos.MemberSign;
import cn.lili.modules.member.entity.dto.MemberPointMessage;
import cn.lili.modules.member.service.MemberSignService;
import cn.lili.modules.wallet.entity.dto.MemberWithdrawalMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author Sylow
 * @Description
 * @Date: Created in 18:03 2025/6/12
 */
@Slf4j
@Service
public class MemberEventHandler {

    /**
     * 会员签到
     */
    @Autowired
    private MemberSignService memberSignService;
    /**
     * 会员积分变化
     */
    @Autowired
    private List<MemberPointChangeEvent> memberPointChangeEvents;
    /**
     * 会员提现
     */
    @Autowired
    private List<MemberWithdrawalEvent> memberWithdrawalEvents;
    /**
     * 会员注册
     */
    @Autowired
    private List<MemberRegisterEvent> memberSignEvents;

    /**
     * 会员注册
     */
    @Autowired
    private List<MemberLoginEvent> memberLoginEvents;
    @Autowired
    private List<MemberInfoChangeEvent> memberInfoChangeEvents;
    @Autowired
    private List<MemberConnectLoginEvent> memberConnectLoginEvents;

    //@Async("tradeEventExecutor")
    @EventListener
    public void handleMemberEvent(MemberEvent event) {
        Member member = null;
        switch (event.getMemberTagsEnum()) {
            //会员注册
            case MEMBER_REGISTER:
                member = (Member) event.getData();
                for (MemberRegisterEvent memberRegisterEvent : memberSignEvents) {
                    try {

                        memberRegisterEvent.memberRegister(member);
                    } catch (Exception e) {
                        log.error("会员{},在{}业务中，状态修改事件执行异常",member.getUsername(),memberRegisterEvent.getClass().getName(), e);
                    }
                }
                break;
            //用户登录
            case MEMBER_LOGIN:
                member = (Member) event.getData();
                for (MemberLoginEvent memberLoginEvent : memberLoginEvents) {
                    try {
                        memberLoginEvent.memberLogin(member);
                    } catch (Exception e) {
                        log.error("会员{},在{}业务中，状态修改事件执行异常",
                                member.getUsername(),
                                memberLoginEvent.getClass().getName(),
                                e);
                    }
                }
                break;
            //会员签到
            case MEMBER_SIGN:
                MemberSign memberSign = (MemberSign) event.getData();
                memberSignService.memberSignSendPoint(memberSign.getMemberId(), memberSign.getSignDay());
                break;
            //会员积分变动
            case MEMBER_POINT_CHANGE:
                MemberPointMessage memberPointMessage = (MemberPointMessage) event.getData();
                for (MemberPointChangeEvent memberPointChangeEvent : memberPointChangeEvents) {
                    try {

                        memberPointChangeEvent.memberPointChange(memberPointMessage);
                    } catch (Exception e) {
                        log.error("会员{},在{}业务中，状态修改事件执行异常", memberPointMessage.getMemberId(),memberPointChangeEvent.getClass().getName(),
                                e);
                    }
                }
                break;
            //会员信息更改
            case MEMBER_INFO_EDIT:
                for (MemberInfoChangeEvent memberInfoChangeEvent : memberInfoChangeEvents) {
                    try {
                        member = (Member) event.getData();
                        memberInfoChangeEvent.memberInfoChange(member);
                    } catch (Exception e) {
                        log.error("会员{},在{}业务中，提现事件执行异常",
                                member.getUsername(),
                                memberInfoChangeEvent.getClass().getName(),
                                e);
                    }
                }
                break;
            //会员提现
            case MEMBER_WITHDRAWAL:
                MemberWithdrawalMessage memberWithdrawalMessage = (MemberWithdrawalMessage)event.getData();
                for (MemberWithdrawalEvent memberWithdrawalEvent : memberWithdrawalEvents) {
                    try {

                        memberWithdrawalEvent.memberWithdrawal(memberWithdrawalMessage);
                    } catch (Exception e) {
                        log.error("会员{},在{}业务中，提现事件执行异常",
                               memberWithdrawalMessage.getMemberId(),
                                memberWithdrawalEvent.getClass().getName(),
                                e);
                    }
                }
                break;
            //用户第三方登录
            case MEMBER_CONNECT_LOGIN:
                MemberConnectLoginMessage memberConnectLoginMessage = (MemberConnectLoginMessage)event.getData();
                for (MemberConnectLoginEvent memberConnectLoginEvent : memberConnectLoginEvents) {
                    try {

                        memberConnectLoginEvent.memberConnectLogin(memberConnectLoginMessage.getMember(), memberConnectLoginMessage.getConnectAuthUser());
                    } catch (Exception e) {
                        log.error("会员{},在{}业务中，状态修改事件执行异常",
                                memberConnectLoginMessage.getMember().getUsername(),
                                memberConnectLoginEvent.getClass().getName(),
                                e);
                    }
                }
                break;
            default:
                break;
        }
    }

}
